/********************************************************************
* Copyright (c) 2013,TGS,All rights reserved.
* fileName:	MapCanvasItem.h	
* created:	2013/04/27 9:11

* version:	1.6
* detail: 
*********************************************************************/

#pragma once

#include <QGraphicsItem>
#include <TGS/GeoMath2.0/Envelope.h>
#include <TGS/GeoMath2.0/Point.h>

class QPainter;

namespace TGS{
namespace MapControl{

class MapCanvas;
class RenderContext;

/** \ingroup gui
* An abstract class for items that can be placed on the
* map canvas.
*/
class MapCanvasItem : public QGraphicsItem
{
public:
	typedef TGS::GeoMath::Envelope Envelope;
	typedef TGS::GeoMath::Point Point;
public:
	//! called on changed extent or resize event to update position of the item
	virtual void updatePosition();

	//! default implementation for canvas items
	virtual QRectF boundingRect() const;

	//! sets current offset, to be called from QgsMapCanvas
	void setPanningOffset( const QPoint& point );

	//! returns canvas item rectangle
	Envelope rect() const;

	//! sets canvas item rectangle
	void setRect( const Envelope& r );

	//! transformation from screen coordinates to map coordinates
	Point toMapCoordinates( const QPoint& point );

	//! transformation from map coordinates to screen coordinates
	QPointF toCanvasCoordinates( const Point& point );

	MapCanvas* mapCanvas() { return _mapCanvas; }

	QPoint panningOffset() { return _panningOffset; }

protected:

	//! protected constructor: cannot be constructed directly
	MapCanvasItem( MapCanvas* mapCanvas );

	virtual ~MapCanvasItem();

	//! function to be implemented by derived classes
	virtual void paint( QPainter * painter ) = 0;

	//! paint function called by map canvas
	virtual void paint( QPainter * painter,
		const QStyleOptionGraphicsItem * option,
		QWidget * widget = 0 );

	//! schedules map canvas for repaint
	void updateCanvas();

	/**Sets render context parameters
	@param p painter for rendering
	@param context out: configured context
	@return true in case of success
	@note added in version 1.5*/
	bool setRenderContextVariables( QPainter* p, RenderContext& context ) const;
private:

	//! pointer to map canvas
	MapCanvas* _mapCanvas;

	//! canvas item rectangle (in map coordinates)
	Envelope _rect;

	//! offset from normal position due current panning operation,
	//! used when converting map coordinates to move map canvas items
	QPoint _panningOffset;

	//! cached size of the item (to return in boundingRect())
	QSizeF _itemSize;
};

}//MapControl
}//TGS